<template>
  <div class="playerMiddle">
    <swiper ref="mySwiper" :options="swiperOptions" class="playerSwiper">
      <swiper-slide class="cd">
        <div class="cd-wrapper" ref="cdWrapper">
          <img :src="this.currentSong.picUrl">
        </div>
        <!--        <p>{{ getFirstLyric() }}</p>-->
        <div class="cd-info">
          <ul ref="smallScreenLyric">
            <li v-for="(value,key) in currentLyric" :key="key" :class="{'active' : currentLineNum === key}">
              {{ value }}
            </li>
          </ul>
        </div>

      </swiper-slide>
      <swiper-slide class="lyric" ref="lyric">
        <scroll-view ref="scrollView">
          <ul class="lyricFull">
            <li v-for="(value,key) in currentLyric" :key="key" :class="{'active' : currentLineNum === key}">
              {{ value }}
            </li>
          </ul>
        </scroll-view>
      </swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
  </div>
</template>

<script>
import {swiper, swiperSlide} from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
import ScrollView from "../../components/ScrollView";
import {mapGetters, mapActions} from "vuex"

export default {
  props: {
    currentTime: {
      type: Number,
      default: 0,
      required: true
    }
  },
  name: "PlayerMiddle",
  components: {
    swiper,
    swiperSlide,
    ScrollView
  },
  data() {
    return {
      swiperOptions: {
        //需要分页器
        pagination: {
          el: '.swiper-pagination',
          bulletClass: 'my-bullet', //自定义分页器类名
          bulletActiveClass: 'my-bullet-active', //自定义分页器类名 => 激活类名
        },
        //告诉 swiper 数据是异步加载的, 数据更新后需要重新加载内容
        observer: true,
        observeParents: true,
        observeSlideChildren: true
      },
      //歌词同步
      currentLineNum: '0',
    }
  },
  methods: {
    ...mapActions([
      'setIsPlaying',
      'getSongLyric',
    ]),
    // 小屏歌词滚动
    smallScreenScroll() {
      // 拿到高亮歌词元素
      const el = document.querySelector('.cd-info li.active')
      // 拿到歌词包裹元素
      if (el === null) return
      this.$refs.smallScreenLyric.style.transform = `translateY(${-el.offsetTop}px)`
    },
    //循环出第一句词
    getFirstLyric() {
      for (let key in this.currentLyric) {
        return this.currentLyric[key]
      }
    },
    //使用递归
    getActiveLineNum(lineNum) {
      //如果歌词的key小于0了, 就不需要递归了.
      if (lineNum < 0) {
        return this.currentLineNum
      }
      let result = this.currentLyric[lineNum + '']
      if (result === undefined || result === '') {
        lineNum--
        return this.getActiveLineNum(lineNum)
      } else {
        return lineNum + ''
      }
    }
  },
  computed: {
    ...mapGetters([
      'isPlaying',
      'currentSong',
      'currentLyric',

    ])
  },
  watch: {
    isPlaying(newValue, oldValue) {
      if (newValue) {
        this.$refs.cdWrapper.classList.add('active')
      } else {
        this.$refs.cdWrapper.classList.remove('active')
      }
    },
    currentTime(newValue, oldValue) {
      // console.log(newValue);
      //1. 高亮歌词同步
      let lineNum = Math.floor(newValue) + '';
      this.currentLineNum = this.getActiveLineNum(lineNum)
      // 2. 歌词滚动同步
      let currentLyricTop = document.querySelector(".lyricFull>li.active").offsetTop
      // console.log(currentLyricTop);
      let lyricHeight = this.$refs.lyric.$el.offsetHeight
      if (currentLyricTop >= (lyricHeight / 2)) {
        //屏幕大于一般,  开始滚动
        this.$refs.scrollView.scrollTo(0, (lyricHeight / 2) - currentLyricTop, 100)
      } else {
        //屏幕不大于一般 ,滚动到顶部
        this.$refs.scrollView.scrollTo(0, 0, 100)
      }
      //3. 半屏歌词滚动
      this.smallScreenScroll()
    },
    currentLyric(newValue, oldValue) {
      for (let key in newValue) {
        this.currentLineNum = key
        return
      }
    }
  },
}
</script>

<style scoped lang="scss">
@import "../../assets/css/mixin";
@import "../../assets/css/variable";

.player {
  .playerSwiper {
    position: fixed;
    top: 150px;
    bottom: 250px;
    left: 0;
    right: 0;

    .cd {
      position: relative;

      .cd-wrapper {
        display: block;
        margin: 0 auto;
        width: 500px;
        height: 500px;
        border-radius: 50%;
        border: 20px solid #fff;
        overflow: hidden;
        animation: sport 40s linear infinite;
        animation-play-state: paused;

        &.active {
          animation-play-state: running;
        }

        img {
          width: 100%;
        }
      }

      .cd-info {
        width: 100%;
        height: 250px;
        overflow: hidden;
        position: absolute;
        top: 600px;

        ul {
          transition: all .5s;

          li {
            opacity: 0.6;
            color: #fff;
            @include font_size($font_medium);
            text-align: center;
            margin: 20px;
            line-height: 60px;
            transition: opacity .5s;

            &.active {
              opacity: 1;
              @include font_size($font_large);
            }
          }
        }
      }

      p {
        text-align: center;
        @include font_size($font_medium);
        color: #fff;
        margin-top: 50px;
      }
    }

    .lyric {
      li {
        text-align: center;
        @include font_size($font_medium);
        color: #fff;
        margin: 0 40px;
        opacity: 0.6;
        transition: opacity 0.3s;
        line-height: 60px;

        &.active {
          opacity: 1;
          @include font_size($font_large);
        }

        &:last-of-type {
          padding-bottom: 50%;
        }
      }
    }

  }
}

@keyframes sport {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
<style lang="scss">
@import "../../assets/css/mixin";
@import "../../assets/css/variable";

.my-bullet {
  display: inline-block;
  width: 20px;
  height: 20px;
  background: #fff;
  border-radius: 50%;
  margin: 0 20px;
  transition: all 0.3s;
  opacity: 0.7;
}

.my-bullet-active {
  width: 50px;
  height: 20px;
  border-radius: 20px;
  @include bullet_active_color();
}
</style>
